package com.huyanqi.front.rest.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.huyanqi.core.Page;
import com.huyanqi.core.RetdCodeType;
import com.huyanqi.core.RetdObj;
import com.huyanqi.core.service.impl.BaseFrontServiceImpl;
import com.huyanqi.dao.DiyDAO;
import com.huyanqi.dao.UserDAO;
import com.huyanqi.entity.Diy;
import com.huyanqi.entity.User;
import com.huyanqi.front.rest.service.DiyRestService;

@Service("DiyRestService")
public class DiyRestImpl extends BaseFrontServiceImpl<Diy,Long, DiyDAO> implements DiyRestService{

	@Override
	@Resource(name="DiyDAO")
	public void setEntityDao(DiyDAO entityDao) {
		this.entityDao=entityDao;
	}
	
	private UserDAO userDao;
	@Resource(name="UserDAO")
	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}
	
	@Override
	public RetdObj<List<Diy>> findLatestDiy(Integer pageSize, Integer pageNum) {
		RetdObj<List<Diy>> retd = new RetdObj<List<Diy>>();
		Page<Diy> page = new Page<Diy>();
		page.setFirstResult((pageNum-1)*pageSize);
		page.setPageSize(pageSize);
		try {
			List<Diy> list = entityDao.findPage(page, "FROM Diy ORDER BY dTime DESC").getResult();
			if(list.size()>0){
				for(Diy d : list){
					User u = userDao.executeQuery("FROM User WHERE uId = ?", d.getDUid()).get(0);
					d.setuName(u.getUNickname());
					d.setuHead(u.getUHead());
				}
				retd.setRealObj(list);
				retd.setCode(RetdCodeType.PASS_OK);
			}else{
				retd.setCode(RetdCodeType.PASS_NODATA);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retd;
	}

}
